#include "enpclient.h"
#include "enpctrl.h"

int CEnpClient::Init(CEnpCtrl& stOwner, CEnpListen& stListen, 
                     SOCKET iSocket, unsigned int uiIP, unsigned short ushPort)
{
    CT_ASSERT(iSocket != INVALID_SOCKET);

    m_pstOwner = &stOwner;
    m_pstListern = &stListen;
    m_stSocket.SetSocket(iSocket);

    // 注册到epoll
    int iRet = Register(
        stOwner,
        &CEnpCtrl::OnClientEvent, 
        stOwner.m_stEpoll,
        m_stSocket.GetSocket(),
        EPOLLIN);
    CHECK_THROW(iRet);

	const CEnpListenInfo& stListenInfo = m_pstListern->GetListenInfo();
	m_stNetHead.uiLocalIP = inet_addr(stListenInfo.m_szListenIP.Get());
	m_stNetHead.unLocalPort = stListenInfo.m_unListenPort;
    m_stNetHead.uiRemoteIP = uiIP;
    m_stNetHead.unRemotePort = ushPort;

    m_stNetHead.uiLastTime = stOwner.m_uiNow;
    m_stNetHead.uiCreateTime = stOwner.m_uiNow;
    m_stNetHead.uiLastTime = m_stNetHead.uiCreateTime;
    m_stNetHead.uiAct1 = 0;
    m_stNetHead.uiAct2 = 0;
    m_stNetHead.unLiveFlag = 0;
    m_stNetHead.ucEnodeMethod = stListenInfo.m_ucEncodeMethod;
    m_stNetHead.ucDeodeMethod = stListenInfo.m_ucDecodeMethod;
    m_stNetHead.szPtKey[0] = '\0';

    return 0;
}
